package com.weipt.rabbitmq;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConnectionUtils {

    public static final String MQ_HOST="localhost";
    public static final int MQ_PORT=5672;
    public static final String MQ_USER="admin";
    public static final String MQ_PWD="admin";
    public static final String MQ_VIRTUAL="/";
    
    public static Connection getMqConn() {
        Connection connection = null;
        try {
            //创建连接mq的连接工厂对象
            ConnectionFactory factory = new ConnectionFactory();
            //设置连接rabbitmq主机、端口
            factory.setHost(MQ_HOST);
            factory.setPort(MQ_PORT);
            //设置访问虚拟主机的用户名和密码
            factory.setUsername(MQ_USER);
            factory.setPassword(MQ_PWD);
            //设置连接那个虚拟主机,此路径用于隔离，类比计算机不同磁盘，或者不同path
            factory.setVirtualHost(MQ_VIRTUAL);
            //获取连接对象
            connection =  factory.newConnection();
        }catch (IOException e){
            System.out.println("IO异常");
        }catch (TimeoutException e){
            System.out.println("连接超时");
        }
        return connection;
    }
}
